package wangjg.sso.util;

import java.net.URL;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DomainUtil {
	private static String getDomainName(String ReferrURL) {
		if ((ReferrURL == null) || ((ReferrURL.trim()).length() == 0))
			return "";

		if (ReferrURL.indexOf('/') < 0)
			return "";

		StringTokenizer stDomain = new StringTokenizer(ReferrURL, "/");
		int count = stDomain.countTokens();
		String[] LinkArr = new String[count];
		for (int i = 0; i < count; i++) {
			LinkArr[i] = stDomain.nextToken();
		}

		String NewLink = "";
		String DomainName = LinkArr[2];

		String nation = "ad,ae,af,ag,ai,al,am,ao,ar,at,au,az,bb,bd,be,bf,bg,bh,bi,bj,bl,bm,bn,bo,br,bs,bw,by,bz,ca,cf,cg,ch,ck,cl,cm,cn,co,cr,cs,cu,cy,cz,de,dj,dk,do,dz,ec,ee,eg,es,et,fi,fj,fr,ga,gb,gd,ge,gf,gh,gi,gm,gn,gr,gt,gu,gy,hk,hn,ht,hu,id,ie,il,in,iq,ir,is,it,jm,jo,jp,ke,kg,kh,kp,kr,kt,kw,kz,la,lb,lc,li,lk,lr,ls,lt,lu,lv,ly,ma,mc,md,mg,ml,mm,mn,mo,ms,mt,mu,mv,mw,mx,my,mz,na,ne,ng,ni,nl,no,np,nr,nz,om,pa,pe,pf,pg,ph,pk,pl,pr,pt,py,qa,ro,ru,sa,sb,sc,sd,se,sg,si,sk,sl,sm,sn,so,sr,st,sv,sy,sz,td,tg,th,tj,tm,tn,to,tr,tt,tw,tz,ua,ug,us,uy,uz,vc,ve,vn,ye,yu,za,zm,zr,zw";
		String kindvprov = "ac,ah,biz,bj,cc,com,cq,edu,fj,gd,gov,gs,gx,gz,ha,hb,he,hi,hk,hl,hn,info,io,jl,js,jx,ln,mo,mobi,net,nm,nx,org,qh,sc,sd,sh,sn,sx,tj,tm,travel,tv,tw,ws,xj,xz,yn,zj";
		int NodeCount = 2;

		StringTokenizer st = new StringTokenizer(DomainName, ".");
		count = st.countTokens();
		String[] NewLinkArr = new String[count];
		for (int i = 0; i < count; i++) {
			NewLinkArr[i] = st.nextToken();
		}
		// 如果倒数第一个，即主域名标识是国家标识
		if (nation.indexOf(NewLinkArr[NewLinkArr.length - 1]) >= 0) {
			// 先确定有三个节点
			NodeCount = 3;
			// 如果国家前一标识不是类型或省级标识，则只有二个节点
			if (kindvprov.indexOf(NewLinkArr[NewLinkArr.length - 2]) < 0)
				NodeCount = 2;
		} else // 否则倒数第一个，即主域名标识是类型标识
		{
			// 也只有两个节点
			NodeCount = 2;
		}
		// 假如类型标识即是主域名，则实际节点数一定小于当前得到的结果
		int istart = NewLinkArr.length - NodeCount;
		if (istart < 0)
			istart = 0;
		// 根据节点数定出的开始节点，取出主域名，这样就不会取到二级域名
		for (int i = istart; i < NewLinkArr.length; i++) {
			// 如果开始节点是WWW，象www.net.cn,www.com.cn,则www根据节点数确定取与不取
			if (!NewLinkArr[i].equalsIgnoreCase("www")) {
				if (NewLink.length() > 0)
					NewLink += ".";
				NewLink += NewLinkArr[i];
			} else {
				// 如果是www，并且节点数为3，这类域名必须要有WWW才能在数据库的URL中找到唯一.
				if (NodeCount == 3) {
					if (i == istart) {
						if (NewLink.length() > 0)
							NewLink += ".";
						NewLink += NewLinkArr[i];
					}
				}
			}
		}

		return NewLink;
	}

	public String getMainDomain(String url) {
		Pattern p = Pattern.compile(
				"(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = p.matcher(url);
		matcher.find();
		return matcher.group();
	}

	public static void main(String args[]) throws Exception {
		String url = "http://anotherbug.blog.chinajavaworld.com.cn/entry/4545/0/";
		url = "http://anotherbug.blog.chinajavaworld.com.cn/";
		URL url2=new URL("http://www1.qq.com.cn");
		System.out.println(url2);

	}
}
